package com.gljx.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gljx.util.CommonExport;
import com.gljx.util.GetFid;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.*;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.GoodstableService;
import com.gljx.web.service.OutwarehousetableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 出库登记Controller
 *
 * @Author: WuLianFa
 * @CreationTime: 2018/6/1 0001 14:42
 */
@Controller
@RequestMapping("/")
public class OutwarehousetableController {
    @Autowired
    private OutwarehousetableService outwarehousetableService;//出库
    @Autowired
    private GoodstableService goodstableService;//物品
    @Autowired
    private DepartmentService departmentService;

    /**
     * 出库登记主界面
     * Created by WuLianFa on 2018/6/1 0001 14:43
     */
    @RequestMapping("/outwarehouse/look.action")
    public Object OutwarehouseMainInterface() {
        return "/backHome/outwarehouseManagement/list";
    }

    /**
     * 分页
     * Created by WuLianFa on 2018/6/1 0001 17:19
     */
    @RequestMapping("/OutwarehouseMassagePaging")
    @ResponseBody
    public Object OutwarehouseMassagePaging(int currentPage, int pageSize, String goodsName, String goodsType, String goodsGgid, String beginTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Outwarehousetable> outwarehousetables = outwarehousetableService.queryAllOutwarehouseRecord(null, beginTime, endTime);//父表
        if (outwarehousetables != null) {
            PageInfo<Outwarehousetable> pageInfo = new PageInfo<>(outwarehousetables);
            for (int i = 0; i < outwarehousetables.size(); i++) {
                outwarehousetables.get(i).setOutwarehousechildren(outwarehousetableService.queryAllOutwarehouseChildRecord(outwarehousetables.get(i).getCkId(), goodsName, goodsType, goodsGgid));
            }
            map.put("getLastPage", pageInfo.getLastPage());//总页数
            map.put("outwarehousetables", outwarehousetables);
        } else {
            map.put("outwarehousetables", null);
        }
        return map;
    }

    /**
     * 跳转到增加出库记录界面
     * Created by WuLianFa on 2018/6/1 0001 14:50
     */
    @RequestMapping("/jumpToAddOutwarehouseRecord")
    public Object jumpToAddOutwarehouseRecord(HttpServletRequest request, String addFlag) {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        String userName = user.getUserName();
        //创建时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String format = df.format(new Date());
        //组织
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        String fnameL2 = (String) department.getFnameL2();
        request.setAttribute("userName", userName);
        request.setAttribute("format", format);
        request.setAttribute("fnameL2", fnameL2);
        request.setAttribute("addFlag", addFlag);
        return "/backHome/outwarehouseManagement/add";
    }

    /**
     * 新增出库登记记录
     * Created by WuLianFa on 2018/6/5 0005 11:25
     */
    @RequestMapping("/insertOutwarehouseMassage")
    public Object insertOutwarehouseMassage(HttpServletRequest request) {
        //父类信息
        Outwarehousetable outwarehousetable = new Outwarehousetable();//父类
        outwarehousetable.setCkId(GetFid.UUIDIntoFid());
        outwarehousetable.setCkSchoolid(request.getParameter("ckSchoolid"));
        outwarehousetable.setCkOutwarehouseuse(request.getParameter("ckOutwarehouseuse"));
        outwarehousetable.setCkOutwarehouseaccepter(request.getParameter("ckOutwarehouseaccepter"));
        outwarehousetable.setCkAuditor(request.getParameter("ckAuditor"));
        outwarehousetable.setCkAuditstatus(request.getParameter("ckAuditstatus"));
        outwarehousetable.setCkCreator(request.getParameter("ckCreator"));
        outwarehousetable.setCkCreationtime(request.getParameter("ckCreationtime"));
        outwarehousetableService.insertOutwarehouseRecordParent(outwarehousetable);
        //子类信息
        String[] ckOutwarehousegoodsnames = request.getParameterValues("ckOutwarehousegoodsname");
        String[] ckOutwarehousegoodstypes = request.getParameterValues("ckOutwarehousegoodstype");
        String[] ckOutwarehouseggids = request.getParameterValues("ckOutwarehouseggid");
        String[] ckOutwarehousegoodsprices = request.getParameterValues("ckOutwarehousegoodsprice");
        String[] ckOutwarehouseprices = request.getParameterValues("ckOutwarehouseprice");
        String[] ckOutwarehousequantities = request.getParameterValues("ckOutwarehousequantity");
        String[] ckOutwarehousetotalprices = request.getParameterValues("ckOutwarehousetotalprices");
        for (int i = 0; i < ckOutwarehousegoodsnames.length; i++) {
            Outwarehousechild outwarehousechild = new Outwarehousechild();
            outwarehousechild.setCkOutwarehousegoodsid(GetFid.UUIDIntoFid());
            outwarehousechild.setCkOutwarehousegoodsname(ckOutwarehousegoodsnames[i]);
            outwarehousechild.setCkOutwarehousegoodstype(ckOutwarehousegoodstypes[i]);
            outwarehousechild.setCkOutwarehouseggid(ckOutwarehouseggids[i]);
            outwarehousechild.setCkOutwarehousegoodsprice(new BigDecimal(ckOutwarehousegoodsprices[i]));
            outwarehousechild.setCkOutwarehouseprice(new BigDecimal(ckOutwarehouseprices[i]));
            outwarehousechild.setCkOutwarehousequantity(new BigDecimal(ckOutwarehousequantities[i]));
            outwarehousechild.setCkOutwarehousetotalprices(new BigDecimal(ckOutwarehousetotalprices[i]));
            outwarehousechild.setCkOutwarehouseid(outwarehousetable.getCkId());
            outwarehousetableService.insertOutwarehouseRecordChild(outwarehousechild);
        }
        return "redirect:/DrivingSys/jumpToAddOutwarehouseRecord?addFlag=1";
    }

    /**
     * 导出出库登记信息excel
     * Created by WuLianFa on 2018/6/4 0004 14:42
     */
    @RequestMapping("/exportOutwarehouseMassageExcel")
    public void exportOutwarehouseMassageExcel(HttpServletResponse response, HttpServletRequest request, String[] ckIdArray) {
        List<Object> list = new ArrayList<>();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //组织
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        String fnameL2 = (String) department.getFnameL2();
        if (ckIdArray == null || ckIdArray.length == 0) {
            List<Outwarehousetable> outwarehousetables = outwarehousetableService.queryAllOutwarehouseRecord(fnameL2, null, null);//父表
            for (int i = 0; i < outwarehousetables.size(); i++) {
                outwarehousetables.get(i).setOutwarehousechildren(outwarehousetableService.queryAllOutwarehouseChildRecord(outwarehousetables.get(i).getCkId(), null, null, null));
            }
            for (int i = 0; i < outwarehousetables.size(); i++) {
                list.add(outwarehousetables.get(i).getCkId());
                list.add(outwarehousetables.get(i).getCkOutwarehouseuse());
                list.add(outwarehousetables.get(i).getCkOutwarehouseaccepter());
                list.add(outwarehousetables.get(i).getCkAuditor());
                list.add(outwarehousetables.get(i).getCkAuditstatus());
                list.add(outwarehousetables.get(i).getCkCreator());
                list.add(outwarehousetables.get(i).getCkCreationtime());
                list.add(outwarehousetables.get(i).getCkLastmodifier());
                list.add(outwarehousetables.get(i).getCkLastmodificationtime());
                list.add(outwarehousetables.get(i).getCkSchoolid());
                list.add(outwarehousetables.get(i).getOutwarehousechildren().get(0).getCkOutwarehousegoodsname());
                list.add(outwarehousetables.get(i).getOutwarehousechildren().get(0).getCkOutwarehousegoodstype());
                list.add(outwarehousetables.get(i).getOutwarehousechildren().get(0).getCkOutwarehouseggid());
                list.add(outwarehousetables.get(i).getOutwarehousechildren().get(0).getCkOutwarehousegoodsprice());
                list.add(outwarehousetables.get(i).getOutwarehousechildren().get(0).getCkOutwarehouseprice());
                list.add(outwarehousetables.get(i).getOutwarehousechildren().get(0).getCkOutwarehousequantity());
                list.add(outwarehousetables.get(i).getOutwarehousechildren().get(0).getCkOutwarehousetotalprices());
                for (int j = 1; j < outwarehousetables.get(i).getOutwarehousechildren().size(); j++) {
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(outwarehousetables.get(i).getOutwarehousechildren().get(j).getCkOutwarehousegoodsname());
                    list.add(outwarehousetables.get(i).getOutwarehousechildren().get(j).getCkOutwarehousegoodstype());
                    list.add(outwarehousetables.get(i).getOutwarehousechildren().get(j).getCkOutwarehouseggid());
                    list.add(outwarehousetables.get(i).getOutwarehousechildren().get(j).getCkOutwarehousegoodsprice());
                    list.add(outwarehousetables.get(i).getOutwarehousechildren().get(j).getCkOutwarehouseprice());
                    list.add(outwarehousetables.get(i).getOutwarehousechildren().get(j).getCkOutwarehousequantity());
                    list.add(outwarehousetables.get(i).getOutwarehousechildren().get(j).getCkOutwarehousetotalprices());
                }
            }
        } else {
            for (int i = 0; i < ckIdArray.length; i++) {
                Outwarehousetable outwarehousetable = outwarehousetableService.selectByckId(ckIdArray[i]);
                outwarehousetable.setOutwarehousechildren(outwarehousetableService.queryAllOutwarehouseChildRecord(outwarehousetable.getCkId(), null, null, null));
                list.add(outwarehousetable.getCkId());
                list.add(outwarehousetable.getCkOutwarehouseuse());
                list.add(outwarehousetable.getCkOutwarehouseaccepter());
                list.add(outwarehousetable.getCkAuditor());
                list.add(outwarehousetable.getCkAuditstatus());
                list.add(outwarehousetable.getCkCreator());
                list.add(outwarehousetable.getCkCreationtime());
                list.add(outwarehousetable.getCkLastmodifier());
                list.add(outwarehousetable.getCkLastmodificationtime());
                list.add(outwarehousetable.getCkSchoolid());
                list.add(outwarehousetable.getOutwarehousechildren().get(0).getCkOutwarehousegoodsname());
                list.add(outwarehousetable.getOutwarehousechildren().get(0).getCkOutwarehousegoodstype());
                list.add(outwarehousetable.getOutwarehousechildren().get(0).getCkOutwarehouseggid());
                list.add(outwarehousetable.getOutwarehousechildren().get(0).getCkOutwarehousegoodsprice());
                list.add(outwarehousetable.getOutwarehousechildren().get(0).getCkOutwarehouseprice());
                list.add(outwarehousetable.getOutwarehousechildren().get(0).getCkOutwarehousequantity());
                list.add(outwarehousetable.getOutwarehousechildren().get(0).getCkOutwarehousetotalprices());
                for (int j = 1; j < outwarehousetable.getOutwarehousechildren().size(); j++) {
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(" ");
                    list.add(outwarehousetable.getOutwarehousechildren().get(i).getCkOutwarehousegoodsname());
                    list.add(outwarehousetable.getOutwarehousechildren().get(i).getCkOutwarehousegoodstype());
                    list.add(outwarehousetable.getOutwarehousechildren().get(i).getCkOutwarehouseggid());
                    list.add(outwarehousetable.getOutwarehousechildren().get(i).getCkOutwarehousegoodsprice());
                    list.add(outwarehousetable.getOutwarehousechildren().get(i).getCkOutwarehouseprice());
                    list.add(outwarehousetable.getOutwarehousechildren().get(i).getCkOutwarehousequantity());
                    list.add(outwarehousetable.getOutwarehousechildren().get(i).getCkOutwarehousetotalprices());
                }
            }
        }
        //头部数据
        String head[] = {"出库编号", "出库用途", "出库接收方", "审核人", "审核状态", "创建人",
                "创建时间", "最后修改人", "最后修改时间", "驾校名称", "物品名称", "物品类型", "物品规格", "物品单价", "物品出库单价", "出库物品数量", "出库物品总价"};
        //长度
        Integer length[] = {5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000};
        try {
            //导出Excel
            CommonExport.importExcel(response, "出库登记表" + TimeFormat.getNowTime2() + ".xls", "出库信息", list, head, length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取物品信息
     * Created by WuLianFa on 2018/6/4 0004 14:54
     */
    @RequestMapping("/outwarehouseGetGoodsMassage")
    @ResponseBody
    public Object outwarehouseGetGoodsMassage(HttpServletRequest request, String goodsName, String goodsType, String goodsGgid) {
        Map<String, Object> map = new HashMap<>();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //组织
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());
        String fnameL2 = (String) department.getFnameL2();
        List<Goodstable> goodstables = goodstableService.goodsAllMassageList(goodsName, goodsType, goodsGgid, fnameL2);
        for (Goodstable goodstable : goodstables) {
            if (goodstable.getWpLastmodifier() == null) {
                goodstable.setWpLastmodifier("");
            }
            if (goodstable.getWpLastmodificationtime() == null) {
                goodstable.setWpLastmodificationtime("");
            }
        }
        map.put("goodstables", goodstables);
        return map;
    }

    /**
     * 选择弹窗物品
     * Created by WuLianFa on 2018/6/4 0004 16:15
     */
    @RequestMapping("/outwarehouseGetGoodsMassageById")
    @ResponseBody
    public Object outwarehouseGetGoodsMassageById(String[] wpIdArray, String wpId) {
        Map<String, Object> map = new HashMap<>();
        if (wpId != null) {
            List<Goodstable> goodstables = new ArrayList<>();
            Goodstable goodstable = goodstableService.selectByPrimaryKey(wpId);
            goodstables.add(goodstable);
            map.put("goodstables", goodstables);
        } else {
            List<Goodstable> goodstables = new ArrayList<>();
            for (int i = 0; i < wpIdArray.length; i++) {
                String s = wpIdArray[i].replace("\"", "").replace("[", "").replace("]", "");
                Goodstable goodstable = goodstableService.selectByPrimaryKey(s);
                goodstables.add(goodstable);
            }
            map.put("goodstables", goodstables);
        }
        return map;
    }

    /**
     * 删除出库登记记录
     * Created by WuLianFa on 2018/6/5 0005 11:25
     */
    @RequestMapping("/deleteOutwarehouseMassage")
    public Object deleteOutwarehouseMassage(String[] ckIdArray, String ckId) {
        //删除记录
        if ("".equals(ckId) || ckId == null) {
            for (int i = 0; i < ckIdArray.length; i++) {
                outwarehousetableService.deleteOutwarehouseMassageParent(ckIdArray[i]);
                outwarehousetableService.deleteOutwarehouseMassageChild(ckIdArray[i]);
            }
        } else {
            outwarehousetableService.deleteOutwarehouseMassageParent(ckId);
            outwarehousetableService.deleteOutwarehouseMassageChild(ckId);
        }
        return "/backHome/outwarehouseManagement/list";
    }

    /**
     * 根据ID查询出库记录
     * Created by WuLianFa on 2018/6/6 0006 09:28
     */
    @RequestMapping("/queryOutwarehouseMassageByckId")
    public Object queryOutwarehouseMassageByckId(HttpServletRequest request, String ckId) {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //父表信息
        Outwarehousetable outwarehousetable = outwarehousetableService.selectByckId(ckId);
        //子表信息
        List<Outwarehousechild> outwarehousechildren = outwarehousetableService.queryAllOutwarehouseChildRecord(ckId, null, null, null);
        outwarehousetable.setOutwarehousechildren(outwarehousechildren);
        request.setAttribute("outwarehousetable",outwarehousetable);
        request.setAttribute("outwarehousechildren",outwarehousechildren);
        return "/backHome/outwarehouseManagement/update";
    }

    /**
     * 修改出库记录
     * Created by WuLianFa on 2018/6/11 0011 15:29
     */
    @RequestMapping("/updateOutwarehouseMassage")
    public Object updateOutwarehouseMassage(HttpServletRequest request) {
        //父类信息
        Outwarehousetable outwarehousetable = new Outwarehousetable();//父类
        outwarehousetable.setCkId(request.getParameter("ckId"));
        outwarehousetable.setCkSchoolid(request.getParameter("ckSchoolid"));
        outwarehousetable.setCkOutwarehouseuse(request.getParameter("ckOutwarehouseuse"));
        outwarehousetable.setCkOutwarehouseaccepter(request.getParameter("ckOutwarehouseaccepter"));
        outwarehousetable.setCkAuditor(request.getParameter("ckAuditor"));
        outwarehousetable.setCkAuditstatus(request.getParameter("ckAuditstatus"));
        outwarehousetable.setCkCreator(request.getParameter("ckCreator"));
        outwarehousetable.setCkCreationtime(request.getParameter("ckCreationtime"));
        outwarehousetableService.updateOutwarehouseRecordParent(outwarehousetable);
        //删除子类
        outwarehousetableService.deleteOutwarehouseMassageChild(outwarehousetable.getCkId());
        //子类信息
        String[] ckOutwarehousegoodsnames = request.getParameterValues("ckOutwarehousegoodsname");
        String[] ckOutwarehousegoodstypes = request.getParameterValues("ckOutwarehousegoodstype");
        String[] ckOutwarehouseggids = request.getParameterValues("ckOutwarehouseggid");
        String[] ckOutwarehousegoodsprices = request.getParameterValues("ckOutwarehousegoodsprice");
        String[] ckOutwarehouseprices = request.getParameterValues("ckOutwarehouseprice");
        String[] ckOutwarehousequantities = request.getParameterValues("ckOutwarehousequantity");
        String[] ckOutwarehousetotalprices = request.getParameterValues("ckOutwarehousetotalprices");
        for (int i = 0; i < ckOutwarehousegoodsnames.length; i++) {
            Outwarehousechild outwarehousechild = new Outwarehousechild();
            outwarehousechild.setCkOutwarehousegoodsid(GetFid.UUIDIntoFid());
            outwarehousechild.setCkOutwarehousegoodsname(ckOutwarehousegoodsnames[i]);
            outwarehousechild.setCkOutwarehousegoodstype(ckOutwarehousegoodstypes[i]);
            outwarehousechild.setCkOutwarehouseggid(ckOutwarehouseggids[i]);
            outwarehousechild.setCkOutwarehousegoodsprice(new BigDecimal(ckOutwarehousegoodsprices[i]));
            outwarehousechild.setCkOutwarehouseprice(new BigDecimal(ckOutwarehouseprices[i]));
            outwarehousechild.setCkOutwarehousequantity(new BigDecimal(ckOutwarehousequantities[i]));
            outwarehousechild.setCkOutwarehousetotalprices(new BigDecimal(ckOutwarehousetotalprices[i]));
            outwarehousechild.setCkOutwarehouseid(outwarehousetable.getCkId());
            outwarehousetableService.insertOutwarehouseRecordChild(outwarehousechild);
        }
        return "/backHome/outwarehouseManagement/list";
    }
}
